unit kwModel;

interface

uses
  InstantPersistence, InstantTypes, IniFiles, SysUtils;

type
  TkwApplication = class;
  TkwConfiguration = class;
  TkwEnvironment = class;
  TkwKeyStat = class;
  TkwProfile = class;
  TkwUser = class;

  TkwApplication = class(TInstantObject)
  {IOMETADATA Name: String(255); }
    _Name: TInstantString;
  private
    function GetName: string;
    procedure SetName(const Value: string);
  published
    property Name: string read GetName write SetName;
  end;

  TkwUser = class(TInstantObject)
  {IOMETADATA stored;
    FirstName: String(255);
    LastName: String(255);
    Password: String(25); }
    _FirstName: TInstantString;
    _LastName: TInstantString;
    _Password: TInstantString;
  private
    function GetFirstName: string;
    function GetLastName: string;
    function GetFullUserName : string;
    function GetPassword: string;
    procedure SetFirstName(const Value: string);
    procedure SetLastName(const Value: string);
    procedure SetPassword(const Value: string);
  published
    property FirstName: string read GetFirstName write SetFirstName;
    property LastName: string read GetLastName write SetLastName;
    property Password: string read GetPassword write SetPassword;
    property FullUserName: string read GetFullUserName;
    property Id;
  end;

  TkwProfile = class(TInstantObject)
  {IOMETADATA stored;
    UserId: Reference(TkwUser);
    Name: String(255); }
    _Name: TInstantString;
    _UserId: TInstantReference;
  private
    function GetName: string;
    function GetUserId: TkwUser;
    procedure SetName(const Value: string);
    procedure SetUserId(Value: TkwUser);
  published
    property Name: string read GetName write SetName;
    property UserId: TkwUser read GetUserId write SetUserId;
  end;

  TkwKeyStat = class(TInstantObject)
  {IOMETADATA stored;
    ProfileId: Reference(TkwProfile);
    Date: Date stored 'DateValue';
    Count: Integer stored 'CountValue';
    App: String(255);
    KeyCode: Integer; }
    _App: TInstantString;
    _Count: TInstantInteger;
    _Date: TInstantDate;
    _KeyCode: TInstantInteger;
    _ProfileId: TInstantReference;
  private
    function GetApp: string;
    function GetCount: Integer;
    function GetDate: TDate;
    function GetKeyCode: Integer;
    function GetProfileId: TkwProfile;
    procedure SetApp(const Value: string);
    procedure SetCount(Value: Integer);
    procedure SetDate(Value: TDate);
    procedure SetKeyCode(Value: Integer);
    procedure SetProfileId(Value: TkwProfile);
  published
    property App: string read GetApp write SetApp;
    property Count: Integer read GetCount write SetCount;
    property Date: TDate read GetDate write SetDate;
    property KeyCode: Integer read GetKeyCode write SetKeyCode;
    property ProfileId: TkwProfile read GetProfileId write SetProfileId;
  end;

  TkwEnvironment = class(TInstantObject)
  {IOMETADATA Config: Part(TkwConfiguration);
    User: Reference(TkwUser); }
    _Config: TInstantPart;
    _User: TInstantReference;
  private
    function GetConfig: TkwConfiguration;
    function GetUser: TkwUser;
    procedure SetConfig(Value: TkwConfiguration);
    procedure SetUser(Value: TkwUser);
  published
    property Config: TkwConfiguration read GetConfig write SetConfig;
    property User: TkwUser read GetUser write SetUser;
  public
    destructor Destroy; override;
  end;

  TkwConfiguration = class(TInstantObject)
  {IOMETADATA DataCatalog: String; }
    _DataCatalog: TInstantString;
  private
    FIniFile : TIniFile;
    function GetDataCatalog: string;
    procedure SetDataCatalog(const Value: string);
  protected
    procedure AfterCreate; override;
    procedure BeforeDispose; override;
  published
    property DataCatalog: string read GetDataCatalog write SetDataCatalog;
  end;

implementation

uses
  InstantMetadata;



const
  CONFIG_FILENAME = 'KeyWatcher.cfg';

{ TkwKey }

{ TkwEnvironment }

{ TkwConfiguration }

procedure TkwConfiguration.AfterCreate;
begin
  inherited;
  FIniFile := TIniFile.Create(CONFIG_FILENAME);
end;

procedure TkwConfiguration.BeforeDispose;
begin
  inherited;

  if Assigned(FIniFile) then
    FreeAndNil(FIniFile);
end;

function TkwConfiguration.GetDataCatalog: string;
begin
  Result := _DataCatalog.Value;
end;

procedure TkwConfiguration.SetDataCatalog(const Value: string);
begin
  _DataCatalog.Value := Value;;
end;

destructor TkwEnvironment.Destroy;
begin
  if Assigned(_User.Value) then _User.Value.Free;

  inherited;
end;

function TkwEnvironment.GetConfig: TkwConfiguration;
begin
  Result := _Config.Value as TkwConfiguration;
end;

function TkwEnvironment.GetUser: TkwUser;
begin
  Result := _User.Value as TkwUser;
end;

procedure TkwEnvironment.SetConfig(Value: TkwConfiguration);
begin
  _Config.Value := Value;;
end;

{ TkwUser }

{ TkwProfile }

function TkwProfile.GetName: string;
begin
  Result := _Name.Value;
end;

function TkwProfile.GetUserId: TkwUser;
begin
  Result := _UserId.Value as TkwUser;
end;

procedure TkwProfile.SetName(const Value: string);
begin
  _Name.Value := Value;;
end;

procedure TkwProfile.SetUserId(Value: TkwUser);
begin
  _UserId.Value := Value;;
end;

function TkwUser.GetFirstName: string;
begin
  Result := _FirstName.Value;
end;

function TkwUser.GetFullUserName: string;
begin
  Result := Format('%s %s', [FirstName, LastName]);
end;

function TkwUser.GetLastName: string;
begin
  Result := _LastName.Value;
end;

function TkwUser.GetPassword: string;
begin
  Result := _Password.Value;
end;

procedure TkwUser.SetFirstName(const Value: string);
begin
  _FirstName.Value := Value;;
end;

procedure TkwUser.SetLastName(const Value: string);
begin
  _LastName.Value := Value;;
end;

procedure TkwUser.SetPassword(const Value: string);
begin
  _Password.Value := Value;;
end;

{ TkwApplication }

function TkwApplication.GetName: string;
begin
  Result := _Name.Value;
end;

procedure TkwApplication.SetName(const Value: string);
begin
  _Name.Value := Value;;
end;

{ TkwKeyStat }

procedure TkwEnvironment.SetUser(Value: TkwUser);
begin
  if Assigned(_User.Value) then
    _User.Value.Free;

  _User.Value := Value;;
end;

function TkwKeyStat.GetApp: string;
begin
  Result := _App.Value;
end;

function TkwKeyStat.GetCount: Integer;
begin
  Result := _Count.Value;
end;

function TkwKeyStat.GetDate: TDate;
begin
  Result := _Date.Value;
end;

function TkwKeyStat.GetKeyCode: Integer;
begin
  Result := _KeyCode.Value;
end;

function TkwKeyStat.GetProfileId: TkwProfile;
begin
  Result := _ProfileId.Value as TkwProfile;
end;

procedure TkwKeyStat.SetApp(const Value: string);
begin
  _App.Value := Value;;
end;

procedure TkwKeyStat.SetCount(Value: Integer);
begin
  _Count.Value := Value;;
end;

procedure TkwKeyStat.SetDate(Value: TDate);
begin
  _Date.Value := Value;;
end;

procedure TkwKeyStat.SetKeyCode(Value: Integer);
begin
  _KeyCode.Value := Value;;
end;

procedure TkwKeyStat.SetProfileId(Value: TkwProfile);
begin
  _ProfileId.Value := Value;;
end;

initialization
  InstantRegisterClasses([
    TkwApplication,
    TkwConfiguration,
    TkwEnvironment,
    TkwKeyStat,
    TkwProfile,
    TkwUser
  ]);

end.
